package org.andao.commons.service.impl;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.andao.commons.service.MailService;
import org.andao.core.exception.ServiceException;
import org.andao.core.utils.ExStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


/**
 * 邮件发送服务
 * @author Seewo Software - Marco.hu(huzhiguo@cvte.cn)
 *
 */
@Service("emailService")
public class MailServiceImpl implements MailService {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	/**邮件发送者*/
	private final static String EMAIL_FROM = "no-reply@cvte.cn";
		
	@Autowired
	@Qualifier("mailSender")
	private JavaMailSender mailSender;
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}
	
	@Override
	public void sendTextMail(String from,String to,String subject,String content) {
		SimpleMailMessage msg = new SimpleMailMessage();
		msg.setFrom(from);
		msg.setTo(to);
		msg.setSubject(subject);
		msg.setText(content);

		try {			
			mailSender.send(msg);
			logger.info("本邮件已发送至{}", StringUtils.arrayToCommaDelimitedString(msg.getTo()));
		} catch (MailException e) {
			logger.error("发送邮件失败", e);
		}
	}
	
	@Override
	public void sendRichMail(String toUserName,String to,String cc,String subject,String content,String attachPath) {
		MimeMessage msg = mailSender.createMimeMessage();

		try {
			MimeMessageHelper helper = new MimeMessageHelper(msg, true);
			helper.setTo(to);
			helper.setFrom(EMAIL_FROM);
			if(!ExStringUtils.isEmpty(cc)){
				helper.setCc(cc);
			}
			helper.setSubject(subject);
			//根据模板构建邮件内容
			//Map<String,Object> map = new HashMap<String,Object>();
			//map.put("userName", toUserName);
			//map.put("content",context);
			//map.put("footer", EMAIL_FOOTER);
			//String content = FreemarkBuildHelper.buildDefaultTemplate("mail.ftl",map);
			helper.setText(ExStringUtils.trimToEmpty(content),true);
			//如果有附件，则添加附件
			if(!ExStringUtils.isEmpty(attachPath)){
				addAttachment(attachPath,helper);
			}
			
			mailSender.send(msg);
			logger.info("HTML版邮件已发送至{}" ,to);
		} catch (MessagingException e) {
			logger.error("构造邮件失败", e);
		} catch (MailException e) {
			logger.error("发送邮件失败", e);
		}
	}
	
	
	/**
	 * 添加邮件附件
	 * @param attachPath 附件路径，为服务器上的全路径
	 * @param helper
	 * @throws MessagingException
	 */
	private void addAttachment(String attachPath,MimeMessageHelper helper) throws MessagingException {
		try {
			File file = new File(attachPath);
			if(!file.exists()){
				throw new ServiceException("文件不存在！");
			}
			helper.addAttachment(file.getName(), file);		
		} catch (Exception e) {
			logger.error("构造邮件失败,附件文件不存在", e);
			throw new MessagingException("附件文件不存在");
		}
	}
	
}
